<!-- Register -->
<template>
  <div class="Login-box">
    <div class="login">
      <p>欢迎加入, 周张恒的博客</p>
      <div class="user">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm">
          <el-form-item prop="username">
            <el-input
              v-model="ruleForm.username"
              placeholder="用户名"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              v-model="ruleForm.password"
              placeholder="密码"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <div class="btn" @click="submit('ruleForm')">注册</div>
          </el-form-item>
        </el-form>
        <router-link to="/home">返回首页</router-link>
      </div>
    </div>
  </div>
</template>

<script>
import { register } from "@/api/user.js";
export default {
  name: "Register",
  data() {
    const username = (rule, val, callback) => {
      if (val === "") {
        return callback(new Error("用户名不能为空"));
      } else if (val.length <= 3) {
        return callback(new Error("用户名太短了"));
      } else callback();
    };
    const password = (rule, val, callback) => {
      if (val === "") {
        callback(new Error("请输入密码"));
      } else if (val.length < 4) return callback(new Error("密码太短了"));
      else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [{ validator: username, trigger: "blur" }],
        password: [{ validator: password, trigger: "blur" }],
      },
    };
  },
  methods: {
    submit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          try {
            let { data } = await register(this.ruleForm);
            console.log(data);
            if (data.status === 501) {
              return this.$message.error(data.msg);
            }
            if (data.status === 200) {
              this.$message.success("注册用户成功");
              this.$store.commit("UPDATE_USER", this.ruleForm);
              this.$router.push("/login");
            }
          } catch (error) {
            console.log(error);
          }
        } else {
          this.$message({
            type: "warning",
            message: "请完整输入表单信息",
          });
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.Login-box {
  height: 100vh;
  background-color: #f6f6f3;
  .login {
    width: 450px;
    height: 300px;
    position: absolute;
    top: 50%;
    bottom: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    p {
      font-weight: 550;
      font-size: 20px;
    }
    p:hover {
      color: rgb(49, 48, 48);
    }
    .user {
      width: 300px;
      margin-left: 75px;
      .btn {
        line-height: 50px;
        background-color: #467b96;
        border-radius: 5px;
        color: white;
        font-size: 18px;
      }
      .btn:hover {
        background-color: #397895;
      }
    }
  }
}
</style>